<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace app\exception;

use app\service\PushService;
use Throwable;
use Webman\Exception\ExceptionHandler;
use Webman\Http\Request;
use Webman\Http\Response;
use plugin\saiadmin\exception\ApiException;

/**
 * Class Handler
 * @package support\exception
 */
class CustomExceptionHandler extends ExceptionHandler
{
    public $dontReport = [
        BusinessException::class,
    ];

    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    public function render(Request $request, Throwable $exception): Response
    {
        //非系统ApiException请求
        if(!($exception instanceof ApiException)){
            $requestData = [
                'msg_type'=>'markdown',
                'markdown_content'=>'<font color="red">错误信息:</font>'.$exception->getMessage()."\n".
                    '<font color="red">文件路径:</font>'.str_replace("\\","/",$exception->getFile())."\n".
                    '<font color="red">文件行号:</font>'.$exception->getLine()."\n".
                    '<font color="red">路由名称:</font>'.$request->path()."\n".
                    '<font color="red">请求参数:</font>'.json_encode($request->all(),JSON_UNESCAPED_UNICODE)
            ];
            $pushService = new PushService();
            $pushService->push($requestData);
        }

        if(($exception instanceof BusinessException) && ($response = $exception->render($request)))
        {
            return $response;
        }
        return parent::render($request, $exception);
    }

}
